﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConverNumToString;

namespace _201206QuanLyBanHang.ChucNang
{
    public partial class danhsachkhachhangduatutheokhuvuc : Form
    {
        public int makv;
        public string tenkv; 
        //private int makh;
        ConvertNumToStr objConvert = new ConvertNumToStr();
        private BLL_Xetdautu db = new BLL_Xetdautu();
        private DataSet ds = new DataSet();
        public danhsachkhachhangduatutheokhuvuc()
        {
            InitializeComponent();
            
            
        }
        private void databind(string sql)
        {

            ds = db.getdautu(sql);

            this.dgdanhsachdautu.DataSource = ds.Tables[0];
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dgdanhsachdautu.Rows[i].Cells["stt"].Value = i + 1;
            }

            dgdanhsachdautu.AllowUserToAddRows = false;

            this.dgdanhsachdautu.AutoResizeColumns(
              DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            this.dgdanhsachdautu.ReadOnly = false;
            dgdanhsachdautu.AllowUserToAddRows = false;
            int dong = 0;
            dong = ds.Tables[0].Rows.Count;
            //this.dgdanhsachdautu.Text = "Số lượng khách hàng được đầu tư: ";
            this.dgdanhsachdautu.Text = dgdanhsachdautu.Text + dong;
            this.grtongdautu.Text = "Số luợng khác hàng được đầu tư tại khu vực " + tenkv + " là: " + dong;

            
        }
        private void danhsachkhachhangduatutheokhuvuc_Load(object sender, EventArgs e)
        {
            string sqlStr = "select b.tencuahang,d.tendautu,sum(c.tongdautu) as tong from khuvuc a, khachhang b,kh_duoc_dt c, dautu d where a.makhuvuc=b.makhuvuc and b.makh=c.makh and c.madautu=d.madautu and b.makhuvuc=" + "'" + makv + "'" +"group by b.tencuahang, d.tendautu order by b.tencuahang,d.tendautu";
            databind(sqlStr);
        }

        private void dgdanhsachdautu_DataSourceChanged(object sender, EventArgs e)
        {
            int tong = 0;
            lblbangchu.Text = "";
            for (int i = 0; i <= dgdanhsachdautu.RowCount-1; i++)
            {
               
               if (dgdanhsachdautu.Rows[i].Cells["tong"].Value != null)
               {
                   string[] a = dgdanhsachdautu.Rows[i].Cells["tong"].Value.ToString().Split('.');
                   tong = tong + int.Parse(a[0]);
               }
            }
            txttongst.Text = string.Format("{0:0,0.00}", tong);
            txttongstiennhap.Text = string.Format("{0:0,0.00}", tong);
            lblbangchu.Text =  "Bằng chữ: "+objConvert.converNumToString(objConvert.slipArray(Convert.ToString(tong.ToString()))) + " đồng";
        }

        private void txttongst_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void cbkhachhang_CheckedChanged(object sender, EventArgs e)
        {
            if (ckkhachhang.Checked)
            {
                cbkhachhang1.Visible = true;
                Btnloc.Visible = true;
                cbkhachhang1.SelectedIndex = -1;
                cbkhachhang1.Text="--chọn khách hàng---";
                db.fillCombo("select makh,tencuahang from khachhang", cbkhachhang1, "tencuahang", "makh");
            }
            else
            {
                cbkhachhang1.Visible =false;
                Btnloc.Visible =false;
                string sqlStr = "select b.tencuahang,d.tendautu,sum(c.tongdautu) as tong from khuvuc a, khachhang b,kh_duoc_dt c, dautu d where a.makhuvuc=b.makhuvuc and b.makh=c.makh and c.madautu=d.madautu and b.makhuvuc=" + "'" + makv + "'" + "group by b.tencuahang, d.tendautu order by b.tencuahang,d.tendautu";
                databind(sqlStr);
            }
        }

        private void Btnloc_Click(object sender, EventArgs e)
        {
             string makh = cbkhachhang1.SelectedValue.ToString();
                string sqlStr = "select b.tencuahang,d.tendautu,sum(c.tongdautu) as tong from khuvuc a, khachhang b,kh_duoc_dt c, dautu d where a.makhuvuc=b.makhuvuc and b.makh=c.makh and c.madautu=d.madautu and b.makhuvuc=" + "'" + makv + "'" + "and b.makh=" + "'" + makh + "'" + "group by b.tencuahang, d.tendautu order by b.tencuahang,d.tendautu";
                databind(sqlStr);
               
        }

        private void cbkhachhang1_SelectedIndexChanged(object sender, EventArgs e)
        {

            Btnloc.Visible = true;
               
        }
    }
}
